//
//  MineTableViewCell.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "MineTableViewCell.h"

@implementation MineTableViewCell

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.leftImage = [[UIImageView alloc] init];
        [self addSubview:self.leftImage];
        self.titleLabel = [[UILabel alloc] init];
        [self addSubview:self.titleLabel];
        
        self.switchControl = [[UISwitch alloc] init];
        [self addSubview:self.switchControl];
        self.rightLabel = [[UILabel alloc] init];
        self.rightLabel.textColor = [UIColor grayColor];
        self.rightLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.rightLabel];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"nightMode" object:nil];
        
        
        
    }
    return self;
}

- (void)changeColor {
    
    NSString *judge = [[NSUserDefaults standardUserDefaults] valueForKey:@"nightMode"];

    if ([judge isEqualToString:@"night"]) {
        self.backgroundColor = [UIColor colorWithRed:29 / 255.0 green:40 / 255.0 blue:52 / 255.0 alpha:1];
        self.titleLabel.textColor = [UIColor whiteColor];
        
        
    } else {
        self.backgroundColor = [UIColor clearColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    

}


- (void)layoutSubviews {
    [super layoutSubviews];
    self.leftImage.frame = CGRectMake(15, 5, self.height - 10, self.height - 10);
    self.titleLabel.frame = CGRectMake(self.leftImage.right + 15, self.leftImage.top, self.width - self.leftImage.width - 5, self.leftImage.height);
    self.switchControl.frame = CGRectMake(self.width - 80, self.leftImage.top, 50, self.height);
    self.rightLabel.frame = CGRectMake(self.switchControl.left, self.leftImage.top, self.width - self.switchControl.left, self.leftImage.height);
    [self.switchControl addTarget:self action:@selector(nightAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)nightAction:(UISwitch *)sender {

   

    
    if (sender.on == YES) {
         [[NSUserDefaults standardUserDefaults] setObject:@"night" forKey:@"nightMode"];
        
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@"sun" forKey:@"nightMode"];
    }
    
     [[NSNotificationCenter defaultCenter] postNotificationName:@"nightMode" object:nil];
    
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
